package com.tgy.JUC.CAS;

import java.util.concurrent.atomic.AtomicInteger;

/***
 * @ClassName: CASDemo
 * @Description: 什么是cas 比较并交换
 * @Auther: 送你一块二向箔
 * @Date: 14:38 2022/3/16
 * @version : V1.0
 */
public class CASDemo {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2022);
        System.out.println(atomicInteger.get());

        //public final boolean compareAndSet(int expect, int update)
        // 期望值达到了就更新，没达到就不更新
        System.out.println(atomicInteger.compareAndSet(2022, 2023));
        System.out.println(atomicInteger.get());

        //期望值2022，现在的值是2022 不一样修改失败
        System.out.println(atomicInteger.compareAndSet(2022, 2023));
        System.out.println(atomicInteger.get());
    }
}
